<template>
  <div class="table-page-search-wrapper">
    <a-card>
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :md="6" :sm="12">
            <a-form-item
                label="考试学年"
            >
              <a-select v-model="queryParam.ksxn">
                <a-select-option v-for="i in TYPE0125" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item
              label="考试学期"
            >
              <a-select v-model="queryParam.ksxq">
                <a-select-option v-for="i in TYPE0072" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12" v-if="routeName === '课程成绩重新发布'||routeName === '课程成绩发布'">
            <a-form-item
              label="学生学号"
            >
              <a-input v-model="queryParam.xsxh"/>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="12">
              <a-form-item
                label="考试类别"
              >
                <a-select v-model="queryParam.kslb">
                  <a-select-option v-for="i in TYPE0109" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item
                label="考试性质"
              >
                <a-select v-model="queryParam.ksxz">
                  <a-select-option v-for="i in TYPE0358" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="开课院系">
                <a-select :disabled="selectMenu.yxList.length === 1 || routeName=='培养办考试成绩审核'" allowClear v-model="queryParam.kcyx" @change="yxChange" showSearch :filterOption="filterOption">
                  <a-select-option v-for="i in selectMenu.yxList" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="课程名称">
                <!--<a-select allowClear v-model="queryParam.kcdm" showSearch :filterOption="filterOptionKc">
                  <a-select-option v-for="(i,index) in selectMenu.courseList" :key="index" :value="i.kch">{{ i.kcmc }}</a-select-option>
                </a-select>-->
                <a-select
                  allowClear
                  v-model="queryParam.kcdm"
                  showSearch
                  :filterOption="filterOption"
                  :notFoundContent="null"
                   >
                  <a-spin v-if="kcLoading" slot="notFoundContent" size="small"/>
                  <a-select-option v-for="(i,index) in selectMenu.courseList" :key="index" :value="i.kch">{{ i.kch }}-{{ i.kcmc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="12" :sm="24">
            <span class="table-page-search-submitButtons" >
            <a-button type="primary" @click="$refs.table.refresh(true)" style="margin:0 10px">查询</a-button>
            <a-button type="primary" @click="queryParam = {}" style="margin:0 10px">重置</a-button>
            <a-button v-if="routeName === '单科成绩打印'" @click="batchPrint" type="primary" style="margin:0 10px">打印成绩单</a-button>
            <a-button v-if="routeName === '单科成绩打印'" @click="batchPrint2" type="primary" style="margin:0 10px">合并打印</a-button>
            <a-button v-if="routeName.includes('考试成绩审核')" @click="batchCheck" type="primary" style="margin:0 10px">批量审核</a-button>
             <!-- <a-button v-hasPermission="'kccj:deleteFinaly'" v-if="routeName.includes('考试成绩审核')" @click="batchDel" type="primary" style="margin:0 10px">删除成绩</a-button> -->
            <a-button v-if="routeName === '课程成绩发布'" @click="batchIssue" type="primary" style="margin:0 10px">成绩发布</a-button>
            <a-button @click="batchBack" style="margin:0 10px" type="primary" v-hasPermission="'cjth:cjth'"
                      v-if="routeName === '课程成绩重新发布'">成绩退回</a-button>
            <a-button v-if="routeName === '未录入成绩查询'" type="primary" style="margin:0 10px" @click="exportExcel">导出</a-button>
            <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

    <a-card :bordered="false" style="margin-top: 10px">
      <s-table
        v-if="initDone"
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :rowSelection="options.rowSelection"
        :pageSize="parameter.pageSize"
        :scroll="{ x: 1300 }"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 + (parameter.pageNo - 1) * parameter.pageSize }}
        </span>
        <span slot="ksrq" slot-scope="text">{{ text && text.slice(0,11) }}</span>
        <template slot="action" slot-scope="xh, record">
          <a @click="showCheck(record, false)">{{ routeName === '课程成绩发布' ? '修改' : '查看' }}</a>
          <span v-if="routeName.includes('考试成绩审核')">
            <a-divider type="vertical"/>
            <a @click="showCheck(record, true)">审核</a>
          </span>
          <span v-if="routeName.includes('课程成绩重新发布')">
            <a-divider type="vertical"/>
            <a @click="rePublish(record)">重新发布</a>
          </span>
        </template>
      </s-table>
    </a-card>

    <checkModal
      v-if="isCheck"
      ref="check"
      :visible="visible"
      @cancel="visible = false"
      @refresh="$refs.table.refresh(false)"
    />

    <showModal
      v-else
      ref="check"
      :visible="visible"
      @cancel="visible = false"
      @refresh="$refs.table.refresh(false)"
    />

    <batch-check-modal :visible="checkVisible" :ids="ids" @cancel="checkVisible = false" @doRefresh="doRefresh" :confirmLoading="checkLoading" />
    <!-- <course-batch-check :visible="checkVisible" :ids="ids" @cancel="checkVisible = false" @doRefresh="doRefresh" :confirmLoading="checkLoading" /> -->

  </div>
</template>

<script>
  import {STable} from '~'
  import {mapState} from 'vuex'
  import checkModal from './components/checkModal'
  import showModal from './components/showModal'
  import batchCheckModal from './components/batchCheck'

  export default {
    data() {
      return {
        advanced: true,
        queryParam: {},
        // columns: [
        //   {
        //     title: '序号',
        //     scopedSlots: { customRender: 'serial' }
        //   },
      //   {
      //     title: '课程代码',
      //     dataIndex: 'kcbm'
      //   },
      //   {
      //     title: '课程名称',
      //     dataIndex: 'kcmc'
      //   },
      //   {
      //     title: '课程类型',
      //     dataIndex: 'kclx'
      //   },
      //   {
      //     title: '开课院系',
      //     dataIndex: 'ksyxmc'
      //   },
      //   {
      //     title: '考试学年',
      //     dataIndex: 'ksxnmc'
      //   },
      //   {
      //     title: '考试学期',
      //     dataIndex: 'ksxqmc'
      //   },
      //   {
      //     title: '考试类别',
      //     dataIndex: 'kslbmc'
      //   },
      //   {
      //     title: '考试性质',
      //     dataIndex: 'ksxzmc'
      //   },
      //   {
      //     title: '考试日期',
      //     dataIndex: 'ksrq'
      //   },
      //   {
      //     title: '操作',
      //     dataIndex: 'id',
      //     scopedSlots: { customRender: 'action' }
      //   }
      // ],
      parameter: {
        pageNo: 1,
        pageSize: 10
      },
      loadData: parameter => {
        this.parameter = parameter
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        let apiFn = this.getCurListApi(this.$route.name)
        return this.$api.cultivate[apiFn](params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectedRowKeys: [],
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      selectMenu: {
        yxList: []
      },
      ids: [],
      visible: false,
      checkVisible: false,
      checkLoading: false,
      isCheck: true,
      kcLoading: false,
      initDone: false
    }
  },
  components: {STable, checkModal, showModal, batchCheckModal},
  computed: {
    ...mapState({
      TYPE0125: state => state.app['TYPE0125'],
      TYPE0072: state => state.app['TYPE0072'],
      TYPE0109: state => state.app['TYPE0109'],
      TYPE0358: state => state.app['TYPE0358']
    }),
    routeName () {
      return this.$route.name
    },
    columns () {
      if (this.routeName !== '未录入成绩查询') {
        return [
          {
            title: '序号',
            scopedSlots: {customRender: 'serial'}
          },
          {
            title: '用户名',
            dataIndex: 'dsxm',
            width: 120,
          },
          {
            title: '用户编号',
            dataIndex: 'dsgh',
            width: 150,
          },
          {
            title: '课程代码',
            dataIndex: 'kcbm'
          },
          {
            title: '课程名称',
            dataIndex: 'kcmc'
          },
          {
            title: '课程类型',
            dataIndex: 'kclbmc'
          },
          {
            title: '开课院系',
            dataIndex: 'ksyxmc'
          },
          {
            title: '考试学年',
            dataIndex: 'ksxn'
          },
          {
            title: '考试年级',
            dataIndex: 'ksnj'
          },
          {
            title: '考试学期',
            dataIndex: 'ksxqmc'
          },
          {
            title: '考试类别',
            dataIndex: 'kslbmc'
          },
          {
            title: '考试性质',
            dataIndex: 'ksxzmc'
          },
          {
            title: '考试日期',
            dataIndex: 'ksrq',
            scopedSlots: { customRender: 'ksrq' },
          },
          {
            title: '操作',
            dataIndex: 'id',
            scopedSlots: { customRender: 'action' },
            width:120,
            fixed: 'right'
          }
        ]
      } else {
        return [

          {
            title: '开课院系',
            dataIndex: 'kcyxmc'
          },
          {
            title: '课题',
            dataIndex: 'kt'
          },
          {
            title: '课程代码',
            dataIndex: 'kcdm'
          },
          {
            title: '课程名称',
            dataIndex: 'kcmc'
          },
          {
            title: '课程类型',
            dataIndex: 'kclxmc'
          },
          {
            title: '班号',
            dataIndex: 'jxbh'
          },
          {
            title: '教师',
            dataIndex: 'jsxm'
          }
          // ,
          // {
          //   title: '成绩状态',
          //   dataIndex: 'kscjztmc'
          // }
        ]
      }
    }
  },
  watch: {
    routeName (val) {
      this.$nextTick(() => {
        this.$refs.table.refresh(true)
        this.selectedRowKeys = []
      })
      if( val == "培养办考试成绩审核" ){
        console.log(this.selectMenu)
        this.queryParam.kcyx = this.selectMenu.yxList[0].dwh
      }
      console.log(val)
    }
  },
  created () {
    this.$store.dispatch('app/setDictionary', 'TYPE0125')
    this.$store.dispatch('app/setDictionary', 'TYPE0072')
    this.$store.dispatch('app/setDictionary', 'TYPE0109')
    this.$store.dispatch('app/setDictionary', 'TYPE0358')
    this.initSelectMenu()
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll(),
        this.$store.dispatch('app/sysInfo')
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yxList: res[0].data
        }
        console.log(res[0].data,111)
        this.queryParam.kcyx = res[0].data[0].dwh
        this.yxChange(this.queryParam.kcyx)
        this.$set(this.queryParam, 'ksxq', res[1].currentSemester)
        this.$set(this.queryParam, 'ksxn', res[1].schoolYear)
        if(this.selectMenu.yxList && this.selectMenu.yxList.length == 1){
          this.$set(this.queryParam, 'kcyx', this.selectMenu.yxList[0].dwh)
          this.yxChange(this.queryParam.kcyx)
        }
        this.initDone = true
      })

    },
    getCurListApi (name) {
      switch (name) {
        case '未录入成绩查询':
          return 'scoreUnRcdList'
        case '培养办考试成绩审核':
          return 'scorePybList'
        case '院系考试成绩审核':
          return 'scoreYxList'
        case '单科成绩打印':
          return 'scoreIssueList'
        case '课程成绩重新发布':
          return 'scoreBackList'
        default:
          return 'scoreList'
      }
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    filterOptionKc (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    // yxChange (code) {
    //   console.log(code)
    //   this.queryParam.kcdm = ''
    //   this.$set(this.selectMenu, 'courseList', [])
    //   if (code) {
    //     this.kcLoading = true
    //     this.$api.cultivate.courseAll({kcssyxm: code}).then(res => {
    //       this.$set(this.selectMenu, 'courseList', res.data)
    //     }).finally(() => {
    //       this.kcLoading = false
    //     })
    //   } else {
    //     this.$set(this.selectMenu, 'courseList', [])
    //   }
    // },
    yxChange (code) {
      this.$set(this.queryParam, 'kcdm', '')
      this.$set(this.selectMenu, 'courseList', [])
      if (code) {
        console.log("yxChange", code)
        this.kcLoading = true
        this.$api.cultivate.selectList4AddCourse({kcssyxm: code}).then(res => {
          this.$set(this.selectMenu, 'courseList', res.data)
        }).finally(() => {
          this.kcLoading = false
        })
      } else {
        this.$set(this.selectMenu, 'courseList', [])
      }
      // const self = this
      // setTimeout(()=>{
      //   self.form.setFieldsValue({kcdm: ''})
      //   self.$set(this.selectMenu, 'courseList', [])
      // },0)
    },
    // FIXME 课程选择
    fetch(value){
      const code = this.queryParam.kcyx
      if (code && value && value.length>=2) {
        this.kcLoading = true
        this.$api.cultivate.selectList4AddCourse({kcssyxm: code, kcmc: value}).then(res => {
          this.$set(this.selectMenu, 'courseList', res.data)
        }).finally(() => {
          this.kcLoading = false
        })
      } else {
        this.$set(this.selectMenu, 'courseList', [])
      }
    },
    handleSearch (value) {
      this.fetch(value)
    },
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    showCheck (data, isCheck) {
      this.isCheck = isCheck
      this.visible = true
      // scoreDetail
      const api = (this.routeName.includes('审核')) ? 'scoreCheckDetail' : 'scoreDetail'
      this.$api.cultivate[api](data.id).then(res => {
        this.$nextTick(() => {
          this.$refs.check.detail = res.data
          this.$refs.check.isYxCheck = this.routeName === '院系考试成绩审核'
          this.$refs.check.form.resetFields()
          this.$refs.check.tableData = res.data.cjksxsmdList
        })
      })
    },
    batchIssue () {
      if (!this.selectedRowKeys.length) {
        this.$message.warning('请选择需要发布的成绩')
        return
      }
      this.$confirm({
        title: '确定发布所选中的成绩信息?',
        content: '当您点击确定按钮后，这些成绩将会被发布',
        centered: true,
        onOk: () => {
          this.$api.cultivate.scoreIssue({id: this.selectedRowKeys.join(',')}).then(res => {
            this.$message.success(res.msg)
            this.selectedRowKeys = []
            this.$refs.table.refresh(false)
            this.$refs.table.clearSelected()
          })
        },
        onCancel: () => {
        }
      })
    },

    rePublish(v) {
      if (!v) {
        this.$message.warning('请选择需要发布的成绩')
        return
      }
      this.$confirm({
        title: '确定发布所选中的成绩信息?',
        content: '当您点击确定按钮后，这些成绩将会被重新发布',
        centered: true,
        onOk: () => {
          this.$api.cultivate.rePublish({id: v.id}).then(res => {
            this.$message.success(res.msg)
            this.$refs.table.refresh(false)
          })
        },
        onCancel: () => {
        }
      })
    },
    batchPrint() {
      if (!this.selectedRowKeys.length) {
        return this.$message.warning('请选择需要打印的成绩')
      }
      this.$export('/foster/kccj/dkcjPrint', {
        ids: this.selectedRowKeys.join(',')
      })
    },
    batchPrint2() {
      if (!this.selectedRowKeys.length) {
        return this.$message.warning('请选择需要合并打印的成绩')
      }
      this.$export('/foster/kccj/dkcjPrintM', {
        ids: this.selectedRowKeys.join(',')
      })
    },
    batchCheck () {
      if (!this.selectedRowKeys.length) {
        this.$message.warning('请选择需要审核的成绩')
        return
      }
      this.ids = this.selectedRowKeys
      this.checkVisible = true
    },
    batchDel() {
      if (!this.selectedRowKeys.length) {
        this.$message.warning('请选择需要删除的成绩')
        return
      }
      this.$confirm({
        title: '提示',
        content:'确定删除选定成绩吗?',
        centered: true,
        onOk: () => {
          var ids = this.selectedRowKeys.join(',')
          this.$api.cultivate.batchDel({id:ids}).then( res=>{
            this.$refs.table.refresh(false)
          })
        },
        onCancel: () => { }
      })
    },
    batchBack () {
      if (!this.selectedRowKeys.length) {
        this.$message.warning('请选择需要退回的成绩')
        return
      }
      this.$confirm({
        title: '确定退回所选中的成绩信息?',
        content: '当您点击确定按钮后，这些成绩将会被退回',
        centered: true,
        onOk: () => {
          this.$message.loading('正在执行退回...', 0)
          this.$api.cultivate.scoreBack({id: this.selectedRowKeys.join(',')}).then(res => {
            this.$message.destroy()
            this.$message.success(res.msg)
            this.selectedRowKeys = []
            this.$refs.table.refresh(false)
            this.$refs.table.clearSelected()
          })
        },
        onCancel: () => {
        }
      })
    },
    doRefresh(){
      this.selectedRowKeys = []
      this.$refs.table.refresh(false)
    },
    exportExcel () {
      let params = this.queryParam
      delete params.pageNum
      delete params.pageSize
      this.$export('/foster/kccj/wlrcjListExp', {
        ...params
      })
    }
  }
}
</script>

<style>

</style>
